
import http, { baseParams } from "@/utils/request";
import { UrlDetectUserInfo } from "@/utils/apiUrl";
import { isEmptyString } from "@/utils/utils";
import { Toast } from "antd-mobile";

/**
 * 查询用户状态
 * @returns thunk
 */
export const detectUserInfo = (phone, password, ipInfo = {}, devTimeZone = "", reqCallback) => {
    return async (dispatch) => {
        console.log("detectUserInfo: " + UrlDetectUserInfo);

        //组装参数
        const params = baseParams()
        params.email = phone || "";
        params.password = password || "";
        params.zjc = "";

        // 格式化headers
        if (ipInfo.timezone) {
            if (ipInfo.timezone === '') {
                ipInfo.timezone = devTimeZone || ""
            }
        } else {
            ipInfo.timezone = devTimeZone || ""
        }
        ipInfo.tz = ipInfo.timezone

        const pHeaders = {
            headers: {
                ...ipInfo,
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*'
            }
        }
        console.log(pHeaders)

        //发起请求
        const res = await http.post(UrlDetectUserInfo, params, pHeaders)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("查询用户状态-结束：=======" + resCode)
        console.log(resObj)
        console.log("查询用户状态-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj)
    }
}
